#include <crt_util.h>

HMODULE dllopen(basic_tchar* path, int flag)
{
#if (TARGET_OS == OS_WIN)
    return LoadLibrary(path);
#elif (TARGET_OS == OS_POSIX)
    return dlopen(path, flag);
#elif (TARGET_OS == OS_MACH)
    return dlopen(path, flag);
#endif
    return 0;
}

void* dllsym(HMODULE handler, char* sym)
{
#if (TARGET_OS == OS_WIN)
    return GetProcAddress(handler, sym);
#elif (TARGET_OS == OS_POSIX)
    void* pFunc = dlsym(handler, sym);
    if (pFunc == NULL) {
        char* pszErr = dlerror();
        printf("%s\n", pszErr);
        return pFunc;
    }
    return pFunc;
#elif (TARGET_OS == OS_MACH)
    void* pFunc = dlsym(handler, sym);
    if (pFunc == NULL) {
        char* pszErr = dlerror();
        printf("%s\n", pszErr);
        return pFunc;
    }
    return pFunc;
#elif (TARGET_OS == OS_UNIX)
    void* pFunc = dlsym(handler, sym);
    if (pFunc == NULL) {
        char* pszErr = dlerror();
        printf("%s\n", pszErr);
        return pFunc;
    }
    return pFunc;
#endif
    return 0;
}

int dllclose(HMODULE handler)
{
#if (TARGET_OS == OS_WIN)
    return FreeLibrary(handler);
#elif (TARGET_OS == OS_POSIX)
    return dlclose(handler);
#elif (TARGET_OS == OS_MACH)
    return dlclose(handler);
#elif (TARGET_OS == OS_UNIX)
    return dlclose(handler);
#endif
    return 0;
}
